<template>
	<view style="padding-top: 100rpx">
		<!-- 订单导航栏 -->
		<view class="order-nav">
			<view class="order-nav-item" :class="{'curr-color':active==i}" v-for="(item,i) in orderNav" :key="i"
				@click="active=i">
				{{item.name}}
			</view>
			<view :style="{'left':leftNum+'%'}" class="curr-line"></view>
		</view>

		<view v-if="orderData.length<1">
			<empty-show imgUrl="close.png" :emptyStyle="'padding-top: 0'">
				<text>暂无订单</text>
			</empty-show>
		</view>
		<view class="order-list">
			<view class="order-item" v-for="item in orderData" :key="item._id">
				<!-- 订单编号及状态 -->
				<view class="order-item-title">
					<text style="color: #999;font-size:22rpx">订单编号：{{item._id}}</text>
					<text class="c-tawny" style="font-size: 26rpx;">{{orderStatus[item.status]}}</text>
				</view>
				<!-- 订单商品 -->
				<view class="order-item-goods">
					<!-- 商品图 -->
					<image class="goods-img" :src="serverUrl+ item.goodsData[0].img_url"></image>
					<!-- 商品信息 -->
					<view class="goods-info">
						<view class="goods-info-name">
							{{item.goodsData[0].name}}
						</view>
						<view class="goods-info-size">{{item.goodsData[0].size}}</view>
						<view class="price-and-count">
							<text style="color:#ee0a24;font-weight: 700;"><text
									style="font-size:28rpx">¥{{item.goodsData[0].price}}</text>.00</text>
							<text>×{{item.goodsData[0].buy_num}}</text>
						</view>
					</view>

				</view>
				<view class="look-goods c-tawny" v-if="item.goodsData.length>1">
					查看全部{{item.goodsData.length}}件商品
				</view>
				<view class="total-price look-goods">
					应付款：<text class="c-tawny">¥{{item.totalPrice}}.00</text>
				</view>
				<view class="order-handle">
					<view class="handle-btn" @click="delOrder(item._id)">
						删除订单
					</view>
					<view class="handle-btn" style="color: #fff;background-color: #ee0a24;" @click="goPay">
						去付款
					</view>
				</view>
			</view>
		</view>


		<!-- 底部更多精选商品 -->
		<view class="goods-more" v-show="active!=0">
			<view class="goods-more-title">
				<view style="padding: 20rpx">更多精选商品</view>
			</view>
			<view class="goods-list">
				<navigator :url="`/subpkg/goods-detail/goods-detail?goodsId=${item._id}`" class="goods-item-box"
					v-for="item in moreList" :key="item._id">
					<image class="goods-img" :src="serverUrl+item.img_url[0]"></image>
					<view class="content">
						<view>{{item.name}}</view>
						<view class="price">￥<text style="font-size: 32rpx">{{item.price}}</text></view>
					</view>
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderNav: [{
						name: '全部'
					},
					{
						name: '待付款'
					},
					{
						name: '待发货'
					},
					{
						name: '待收货'
					},
					{
						name: '待评价'
					}
				],
				active: 0,
				leftNum: 0,
				moreList: [], //更多商品列表
				orderTotalData: [], // 所有订单
				orderStatus: [
					'交易关闭',
					'等待买家付款',
					'等待卖家发货',
					'等待确认收货',
					'订单未评价'
				],
			};
		},
		watch: {
			active(newVal, oldVal) {
				let steps = (newVal - oldVal) * 20;
				this.changeLeft(steps);
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			}
		},
		computed: {
			orderData() { // 当前订单列表
				let val = [];
				if (this.active != 0) {
					val = this.orderTotalData.filter(item => item.status == this.active)
				} else {
					val = this.orderTotalData;
				}
				return val
			}
		},
		onLoad(options) {
			if (options.active) {
				this.active = options.active - 0;
			}
			this.getMoreList();
			this.getOrderData();
		},
		onShow() {
			this.getOrderData();
		},
		methods: {
			// 改变curr-line定左偏移位置
			changeLeft(steps, count = 5) {
				let step = steps / count;
				let timer = setInterval(() => {
					if (count <= 0) {
						clearInterval(timer);
						return
					}
					this.leftNum += step;
					count--;
				}, 10)
			},
			async getMoreList() { // 获取更多商品推荐数据
				let {
					data
				} = await uni.$http.reqMoreList();
				this.moreList = data;
			},
			async getOrderData() { // 获取订单信息
				let {
					data
				} = await uni.$http.reqOrderData(this.user_id);
				this.orderTotalData = data;
			},
			delOrder(_id) { //根据订单号删除订单
				uni.$http.reqDelOrder(_id).then(() => {
					uni.showToast({
						title: '删除订单成功',
						icon: "none"
					});
					this.getOrderData();
				})
			},
			goPay() {
				uni.showToast({
					title: '暂无支付功能',
					icon: 'none'
				})
			}
		}
	}
</script>

<style lang="scss">
	.order-nav {
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		justify-content: space-around;
		width: 100vw;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 28rpx;
		background-color: #fff;
		z-index: 9999;

		.order-nav-item {
			flex: 1;
		}

		.curr-line {
			position: absolute;
			bottom: 10rpx;
			transform: translateX(40%);
			width: 84rpx;
			height: 6rpx;
			background-color: $my-tawny;
		}

		.curr-color {
			color: $my-tawny;
		}
	}

	.order-list {
		.order-item {
			margin-top: 20rpx;
			background-color: #fff;

			.order-item-title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 60rpx;
				padding: 0 20rpx;
			}

			.order-item-goods {
				display: flex;
				padding: 25rpx;

				.goods-img {
					width: 160rpx;
					height: 160rpx;
					margin-right: 15rpx;
				}

				.goods-info {
					flex: 1;
					padding: 10rpx 0;
					font-size: 22rpx;

					.goods-info-name {
						font-size: 26rpx;
						font-weight: 700;
					}

					.goods-info-size {
						padding: 10rpx 0 40rpx;
						color: #999;
					}

					.price-and-count {
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}

			}

			.look-goods {
				padding: 20rpx 0;
				font-size: 26rpx;
				text-align: center;
				border-bottom: 1px solid #ebedf0;
			}

			.total-price {
				padding: 20rpx;
				text-align: right;
			}

			.order-handle {
				display: flex;
				justify-content: flex-end;
				padding: 15rpx 0;
				font-size: 22rpx;

				.handle-btn {
					height: 50rpx;
					padding: 0 15rpx;
					margin-right: 20rpx;
					line-height: 50rpx;
					border: 1px solid #ebedf0;
					border-radius: 25rpx;
				}
			}
		}
	}

	.goods-more {
		padding: 0 20rpx;

		.goods-more-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 26rpx;

			&::before,
			&::after {
				content: '';
				flex: 1;
				height: 2rpx;
				background-color: #dcdee0;
			}
		}

		.goods-list {
			display: flex;
			justify-content: space-between;
			align-content: space-between;
			flex-wrap: wrap;

			.goods-item-box {
				width: 49%;
				margin-bottom: 20rpx;
				box-sizing: border-box;
				background-color: #fff;
				border-radius: 20rpx;

				.goods-img {
					width: 100%;
					height: 174px;
				}

				.content {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					box-sizing: border-box;
					height: 160rpx;
					padding: 20rpx;
					font-size: 25rpx;

					.price {
						color: $my-tawny;
					}
				}
			}
		}
	}
</style>
